//controller
Ext.define('HIT.controller.PersonalSetting',{
	extend:'Ext.app.Controller',
	views:['SetPersonalImage','SetRepasswordPanel'],
	
	onBeforeAdd: function(comp, options){
//		Ext.MessageBox.alert("CLS:"+Ext.getCmp("actionbar").cls);
	},
	
	onButtonClick: function(button, e, options) {		
		if(button.id == 'set-personal-image-save'){  //保存按钮
			var RadioGroup = Ext.getCmp('radio-img-group');
			var RadioValue = null;
			RadioGroup.items.each(function(item){
				if(item.checked){
					RadioValue = item.inputValue;
				}
			});
//			var DOC = document.getElementsByTagName('body')[0];
//			var Old_Personal_Image_String = DOC.childNodes[0].childNodes[1].className;
//			var Old_RadioCSS = Ext.getCmp('radio-img-set-value').getValue();
//			var New_Personal_Image_String = Old_Personal_Image_String.replace(Old_RadioCSS,RadioValue);
			Share.AjaxRequest({  //保存图片信息到后台
				url : ctx + '/PersonalImageSave',
		    	params : {
		    		CompanyImg : RadioValue
		    	},
		    	callback : function(json) {//设置成功后处理函数		    		
		    		if(json.resultcode == '0'){
		    			Share.ShowMessage("I0006","修改");
		    			document.getElementById("test_act_img").setAttribute("src", RadioValue);
//		    			DOC.childNodes[0].childNodes[1].className = New_Personal_Image_String;
		    			Ext.getCmp('radio-img-set-value').setValue(RadioValue);
		    		}else if(json.resultcode == '-1'){
		    			Share.ShowMessage("I0007","修改");
		    		}else if(json.resultcode == '-99'){
		    			Share.ShowMessage("I0027");
		    		}else{
		    			Ext.Msg.alert(Share.T('提示'), json.resultcode);
		    		}
		    	}
			});
		}else if(button.id == 'set-modify-password'){//修改密码 2013.05.27 BY guxiaoli
			var panel = Share.createPanelByAlias('widget.setrepasswordpanel'); 
	//		Share.showPanelAsWindow(panel,true);
			Share.showPanelAsWindow(panel);
		}
	},
	OnBeforeRender: function(A,B){
//		console.log("aaa");
			 Share.AjaxRequest({  //系统登录时读取
   			url : ctx + '/PersonalImage',
   	    	params : {},
   	    	callback : function(json) {//读取图片成功后，创建N个radio到一个radiogroup中
   	    		if(json.length > 0){//使用指定图片
   	    			var Image = json[0].ImageUrl;
   	    			var ImageButtonGroup = Ext.getCmp('radio-img-group');        
   	    			ImageButtonGroup.items.each(function(item){
   	    				if(item.inputValue == Image){
//   	    					console.log("item.value: "+item.value);
   	    					item.setValue(true);
   	    				}
   	    			});
   	    		}       	    		
   	    	}
   		});
	},
	
	save : function(win) {
   	 var grid=Ext.getCmp('tab-set-image');       
        if (grid == undefined) {
 			return;
 		}
        var form = Ext.getCmp('panel-setrepasswordpanel');
        var basicForm = form.getForm();
        if (!basicForm.isValid()) {
            return;
        }
   	  var origin = Ext.getCmp('repassword-origin').getValue();
   	  var newPassword = Ext.getCmp('repassword-new').getValue();
   	  if(origin == newPassword){
   		Share.ShowMessage("E0068");
   		return;
   	  }
   	  Ext.MessageBox.confirm(Share.T('请确认'),Share.T('确定修改密码吗?'), function(button, text) {
         if (button == 'yes') {
        	 Ext.Ajax.request({//重置密码ajax请求
				url : ctx + '/setModifyPassword', 
				params : {origin      : Ext.MD5(origin)
					     ,newPassword : Ext.MD5(newPassword)
					     },
				success : function(response) {
	    	    	json = Ext.JSON.decode(response.responseText);
					if (json.resultcode == '0') {
						form.up('window').close();
						Share.ShowMessage("I0006","");
					}else if(json.resultcode == '2'){
						Share.ShowMessage("E0017");
						Ext.getCmp('repassword-origin').focus(true,true); 
					}else {
						Share.ShowMessage("I0007","");
						}
				},
				falseFun : function(json) {Share.ShowMessage("I0007","");}
			});
           }
       });   	  	     
	},
	
	init: function(application) {
		this.control({
			"button": {
                click: this.onButtonClick
           },
           "form":{
        	   beforeadd:this.onBeforeAdd
           },
           "#tab-set-image": {
        	   beforeadd:this.OnBeforeRender
           }
		});
	}
});